$(function () {
    var calendar = {
        dayTable: null, //初始化TABLE 
        year: null, //初始化年 
        month: null, //初始化月份 

        getFirstDay: function (year, month) { //获取每个月第一天再星期几 
            var firstDay = new Date(year, month, 1);
            return firstDay.getDay(); //getDay()方法来获取 
        },
        getMonthLen: function (year, month) { //获取当月总共有多少天 
            var nextMonth = new Date(year, month + 1, 1); //获取下个月的第一天 
            nextMonth.setHours(nextMonth.getHours() - 3); //由于获取的天是0时,所以减去3小时则可以得出该月的天数 
            return nextMonth.getDate(); //返回当天日期 
        },
        createCalendar: function (form, date) { //创建日历方法 
            calendar.year = date.getFullYear(); //获得当时的年份,并赋值到calendar属性year中,以便别的方法的调用 
            calendar.month = date.getMonth(); //跟上面获取年份的目的一样 
            $('#calendar_year').text(calendar.year);
            $('#calendar_month').text(calendar.month + 1); //插入年份和月份 
            // calendar.clearCalendar(form); //清空TABLE 
            var monthLen = calendar.getMonthLen(calendar.year, calendar.month); //获取月份总共有多少天 
            var firstDay = calendar.getFirstDay(calendar.year, calendar.month); //获取月份首天为星期几 
            var html;
            for (var j = 0; j < firstDay; j++) { //循环写入每天的值进入table中 
                html = '<tr><td></td>';
            }
            for (var i = 1; i <= monthLen; i++) { //循环写入每天的值进入table中 
                var weekend = (new Date(calendar.year, calendar.month, i)).getDay();
                if (weekend === 0) {
                    html += '<tr>';
                }
                // calendar.dayTable[i + firstDay - 1].innerHTML = i; //i为循环值,加上第一天的星期值刚可以对应TABLE位置,但由于数组从0开始算,所以需要减去1 
                // if ((i + firstDay - 2) == new Date().getDate() && calendar.month == new Date().getMonth() && calendar.year == new Date().getFullYear()) { //判断是否是当天 
                //     // calendar.dayTable[i + firstDay - 3].id = 'today';
                //     html += "<td id='today'>" + i + "</td>";  
                // }else{
                html += "<td class='day_" + i + "'>" + i + "</td>";
                // }                
                if (weekend === 6) {
                    html += '</tr>';
                } else if (i === monthLen && weekend !== 6) {
                    var blankNum = 6 - weekend;
                    for (var a = 0; a < blankNum; a++) {
                        html += '<td></td>';
                    }
                    html += '</tr>';
                }

            }
            $('#eui_calendar_body').html(html);
        },
        clearCalendar: function (form) { //清空TABLE方法 
            $('#eui_calendar_body').html('');
            // this.dayTable = form.getElementsByTagName('td');
            // for (var i = 0; i < this.dayTable.length; i++) {
            //     this.dayTable[i].innerHTML = ' ';
            //     this.dayTable[i].id = '';
            // }
        },
        init: function (form) { //主方法 
            this.dayTable = form.getElementsByTagName('td');
            this.createCalendar(form, new Date());
            var preMon = $(".eui-calendar-header span").eq(0);
            var nextMon = $(".eui-calendar-header span").eq(2);
            preMon.onclick = function () { //当点击左按钮时,减去一个月,并重绘TABLE 
                calendar.createCalendar(form, new Date(calendar.year, calendar.month - 1, 1));
            }
            nextMon.onclick = function () { //当点击右按钮时,加上一个月,并重绘TABLE 
                calendar.createCalendar(form, new Date(calendar.year, calendar.month + 1, 1));
            }
        }

    }
    window.onload = function () {
        var calendars = document.getElementById('eui_calendar');
        calendar.init(calendars);
    }
});